package com.atcc.demo.common;

import java.util.UUID;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.atcc.demo.domain.TokenModel;
import com.atcc.demo.domain.UserInfo;
import com.atcc.demo.service.UserTokenService;


@Component
public class TokenHelper {
	
	private static TokenHelper instance = null;
	
	@Autowired
	UserTokenService userTokenService;
	
	@PostConstruct
	public void init()
	{
		instance=this;
	}
	
    private TokenHelper() {};
	
	public static TokenHelper getInstance()
	{
		if(instance==null)
		{
			synchronized(TokenHelper.class)
			{
				if(instance==null)
				{
					instance=new TokenHelper();
				}
			}
		}
		return instance;
	}
	
	//登陆创建Token
	public TokenModel create(UserInfo user,boolean isphone) {
		TokenModel tm = new TokenModel();
		String tokenkey = "";
		if(isphone) {
			tokenkey = "usertoken_phone_" + user.getphonenumber();
		}else {
			tokenkey = "usertoken_" + user.getphonenumber();
		}
		String token = UUID.randomUUID().toString().replace("-", "");
		tm.setTokenCode(token);
		tm.setName(user.getName());
		tm.setPassword(user.getpassword());
		tm.setphonenumber(user.getphonenumber());
		return tm;
		
	}

}
